"""
    Author:
    Date: 2018-10-30

    微信管理后台-设置页面
"""

from django.shortcuts import render

from utils import ajax
from utils import wx_config
from utils.log import Log
from utils.wx_util import WechatApi

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def index(request):
    """ 设置页面 """
    context = helpers.build_context(request)
    context['username'] = request.user.username
    return render(request, 'gma/settings/settings.html', context)


@gma_login_required
def user_tags(request):
    """ 用户标签页面 """
    context = helpers.build_context(request)
    context['username'] = request.user.username
    context['tags'] = WechatApi.get_user_tags()
    return render(request, 'gma/settings/settings_user_tags.html', context)


@gma_login_required
def custom_menu(request):
    """ 设置管理员个性化菜单页面 """
    context = helpers.build_context(request)
    context['username'] = request.user.username
    context['menu'] = wx_config.CUSTOM_MENU_FOR_EVERYONE
    return render(request, 'gma/settings/settings_custom_menu.html', context)


@gma_login_required
def custom_admin_menu(request):
    """ 设置管理员个性化菜单页面 """
    context = helpers.build_context(request)
    context['username'] = request.user.username
    context['menu'] = wx_config.CUSTOM_MENU_FOR_ADMIN
    return render(request, 'gma/settings/settings_custom_admin_menu.html', context)


# ------------------------------------------------------------------------------
# API
# ------------------------------------------------------------------------------

@gma_login_required
def api_set_custom_menu(request):
    """ 设置通用的个性化菜单 """
    success = WechatApi.set_menu(wx_config.CUSTOM_MENU_FOR_EVERYONE)
    if success:
        Log.info(f'Set wx menu success: admin={request.user.username}')
    else:
        Log.error(f'Set wx menu failed: admin={request.user.username}')

    return ajax.response(success, error='设置通用的用户个性化菜单失败!')

@gma_login_required
def api_set_custom_admin_menu(request):
    """ 设置通用的管理员个性化菜单 """
    success = WechatApi.set_menu(wx_config.CUSTOM_MENU_FOR_ADMIN)
    if success:
        Log.info(f'Set wx menu success: admin={request.user.username}')
    else:
        Log.error(f'Set wx menu failed: admin={request.user.username}')

    return ajax.response(success, error='设置通用的管理员个性化菜单失败!')


if __name__ == '__main__':
    print('ok')

